package first.progr.tc;
import static first.progr.tc.TestBase.generateRandStr;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.thoughtworks.xstream.XStream;

public class GroupDataGenerator {
	
	
   public static void main(String[] args) throws IOException {
	 Scanner in = new Scanner(System.in);
	 System.out.println("Enter string in format:<acount od test data> <file> <format>");
	 String[] inputString = in.nextLine().split(" ");
	   if (inputString.length < 3) {
	      System.out.println("Format of entered line is incorrect. Please repeat entering one more time ");
	      return;
       } 
     int amount = Integer.parseInt(inputString[0]);
     File file = new File(inputString[1]);
     String format = inputString[2];
	
     if (file.exists()) {
	    System.out.println("File " + file + " already exists, please remove it manualy");
	    return;
     }
     
     List<GroupData> groups = generateRandomGroups(amount);
     if ("csv".equals(format)) {
	     saveGroupsToCsvFile(groups, file);
     } else if ("xml".equals(format)) {
	     saveGroupsToXmlFile(groups, file);  
     } else {
	     System.out.println("Unknown " + format + " file format ");
	     return;
     }
	 
     System.out.println("File " + file + " was sucessfully generated");
  }

  public static List<GroupData> generateRandomGroups(int amount) {
	  List<GroupData> list = new ArrayList<GroupData>();
	    for (int i = 0; i < amount; i++) {
	    	GroupData group = new GroupData()
	    	  .withName(generateRandStr())
	    	  .withHeader(generateRandStr())
	    	  .withFooter(generateRandStr());
	    	list.add(group);
	    }
	    return list;
  }
  
  private static void saveGroupsToCsvFile (List<GroupData> groups, File file) throws IOException {
      FileWriter writeF = new FileWriter(file);
      for (GroupData gr : groups){
    	 writeF.write(gr.getName() + "," +gr.getHeader() + "," + gr.getFooter() + ",!" + "\n"); 
      }
      writeF.close();
  }
  
  public static List<GroupData> loadGroupsFromCsvFile(File file) throws IOException {
	  List<GroupData> list = new ArrayList<GroupData>();
	  FileReader readerF = new FileReader(file);
	  BufferedReader bufferedReader = new BufferedReader(readerF);
	  String line = bufferedReader.readLine();
	  while (line != null) {
		  String[] part = line.split(",");
		  GroupData gr = new GroupData()
		    .withName(part[0])
		    .withHeader(part[1])
		    .withFooter(part[2]);
		  list.add(gr);
		  line = bufferedReader.readLine();
	  }
	  bufferedReader.close();
	  return list;
  }

  public static List<GroupData> loadGroupsFromXmlFile(File file) throws IOException {
	  XStream xstr = new XStream();
	  xstr.alias("group", GroupData.class);
	  return (List<GroupData>) xstr.fromXML(file);
  }
  
  private static void saveGroupsToXmlFile (List<GroupData> groups, File file) throws IOException {
	  XStream xsParser = new XStream();
	  xsParser.alias("group", GroupData.class);
	  String xml = xsParser.toXML(groups);
	  FileWriter writerF = new FileWriter(file);
	  writerF.write(xml);
	  writerF.close();
  }
}
